import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.text.DecimalFormat;

import javax.swing.*;

@SuppressWarnings("serial")//一系列的限制运行的指令
public class MainFrame4 extends JFrame implements ActionListener {//监听器
	Vector<Film> filmList=new Vector<Film>();//定义电影文件
	Vector<Music> musicList=new Vector<Music>();//音乐文件
	Vector<Comment> commentList=new Vector<Comment>();//评论文件
	JMenu Menu0=new JMenu("文件");
	JMenuItem menuOpen=new JMenuItem("打开数据文件");
	JMenuItem menuSave=new JMenuItem("保存数据文件");
	JMenuItem menuChangePwd=new JMenuItem("更改管理密码");
	JMenuItem menuExit=new JMenuItem("退出系统");
	JMenu Menu1=new JMenu("信息维护");
	JMenuItem filmMenu=new JMenuItem("影片信息维护");
	JMenuItem musicMenu=new JMenuItem("音乐信息维护");
	JMenuItem commentMenu=new JMenuItem("评价信息维护");
	JMenu Menu2=new JMenu("数据查询");
	JMenuItem filmQueryMenu=new JMenuItem("影片信息查询");
	JMenuItem commentQueryMenu=new JMenuItem("评价信息查询");
	
	public static void main(String[] args) {
		LoginFrame f=new LoginFrame();
		f.setVisible(true);//设置可视觉化
	}
	
	MainFrame4(){
		super();
		JMenuBar menuBar=new JMenuBar();//设置菜单按钮
		this.setJMenuBar(menuBar);//定位当前菜单按钮
		filmMenu.addActionListener(this);//添加电影菜单的监视器
		filmMenu.setEnabled(false);//添加电影菜单的可操作按钮
		Menu1.add(filmMenu);//菜单1 电影菜单
		musicMenu.addActionListener(this);//添加音乐菜单的监视器
		musicMenu.setEnabled(false);//添加音乐菜单的可操作按钮
		Menu1.add(musicMenu);//菜单 音乐菜单
		musicMenu.addActionListener(this);//添加音乐菜单的监视器
		musicMenu.setEnabled(false);//添加音乐菜单的可操作按钮
		Menu1.add(musicMenu);//添加音乐菜单
		menuOpen.addActionListener(this);//打开菜单后启动动作追踪监视器
		Menu0.add(menuOpen);
		menuSave.addActionListener(this);
		menuSave.setEnabled(false);
		Menu0.add(menuSave);//保存数据文件的菜单按钮
		Menu0.addSeparator();//添加分离器
		Menu0.add(menuChangePwd);//添加菜单转换的显示目录指令
		menuChangePwd.addActionListener(this);//目录显示指令下的动作追踪监视器
		Menu0.addSeparator();
		menuExit.addActionListener(this);
		Menu0.add(menuExit);//菜单退出
		filmQueryMenu.addActionListener(this);
		filmQueryMenu.setEnabled(false);
		Menu2.add(filmQueryMenu);//电影查询菜单
		commentQueryMenu.addActionListener(this);
		commentQueryMenu.setEnabled(false);
		Menu2.add(commentQueryMenu);//评论查询菜单
		menuBar.add(Menu0);
		menuBar.add(Menu1);
		menuBar.add(Menu2);
		
		this.setTitle("电影资源管理系统");//标题
		this.setResizable(false);//窗口可重置按钮
		this.setSize(700,320);//窗口大小
		this.setLocationRelativeTo(this.getOwner());//相关位置
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//在关闭窗口时终止该程序
	}
	
	//读取电影文件
	void readFilmFile(){
		try {
			FileInputStream fis = new FileInputStream("Film.txt");//输入文件来源
			InputStreamReader dis=new InputStreamReader(fis);//读取电影文件
			BufferedReader reader=new BufferedReader(dis);//缓存读物
			String s;
			while((s=reader.readLine())!=null)
			{
				Film film=new Film();
				String[] temp=s.split(" ");
				film.setFilmName(temp[0]);//电影名称
				film.setFilmDirector(temp[1]);//电影导演
				film.setFilmTime(temp[2]);//电影上映时间
				film.setFilmActors(temp[3]);//电影演员
				filmList.add(film);//电影查询菜单
			}
			reader.close();//关闭读物
			dis.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace(); //打印异常
		}
	}
	
	
	//写入电影文件
	void writeFilmFile(){
		try {
			
			//文件字节输出流
			FileOutputStream fos = new FileOutputStream("Film.txt");
			
		//将字节流转换为字符流
			OutputStreamWriter dos=new OutputStreamWriter(fos);
			
			//带有默认缓冲的字符输入流
			BufferedWriter writer=new BufferedWriter(dos);
			for(int i=0;i<filmList.size();i++){
				Film film=(Film)filmList.get(i);
				
				//输入电影文件的“电影名称”“导演”“演员”“上映时间”
				writer.write(film.getFilmName()+" "+film.getFilmDirector()+" "+film.getFilmName()+" "+film.getFilmActors()+"\n");
			}
			writer.close();
			dos.close();
			fos.close();
			
			//在不存在具有指定路径名的文件时抛出此异常
		} catch (FileNotFoundException e) {
			
		//打印异常
			e.printStackTrace();
			
			//（在读写数据时）发生 I/O 错误时引发的异常
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	void readMusicFile(){
		try {
			//FileInputStream 用于读取本地文件中的字节数据,继承自InputStream类
			FileInputStream fis = new FileInputStream("Music.txt");
			
			//InputStreamReader()方法是字节流通向字符流的桥梁，它使用指定的 charset读取字节并将其解码为字符
			InputStreamReader dis=new InputStreamReader(fis);
			
			//BufferedReader 由Reader类扩展而来，提供通用的缓冲方式文本读取，而且提供了很实用的readLine，读取一个文本行，从字符输入流中读取文本，缓冲各个字符，从而提供字符、数组和行的高效读取。
			BufferedReader reader=new BufferedReader(dis);
			String s;
			while((s=reader.readLine())!=null)
			{
				Music music=new Music();
				String[] temp=s.split(" ");
				music.setMusicTheme(temp[0]);//主题曲
				music.setMusicTime(temp[1]);//歌曲时长
				music.setMusicMusician((temp[2]));//歌手
				music.setCriticism(temp[3]);//乐评
				musicList.add(music);//歌曲目录
			}
			reader.close();//关闭读物
			dis.close();
			fis.close();
			
			//（在读写数据时）发生 I/O 错误时引发的异常
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	void writeMusicFile(){
		try {
			
			//文件字节输出流
			FileOutputStream fos = new FileOutputStream("Music.txt");
			
			//将字节流转换为字符流
			OutputStreamWriter dos=new OutputStreamWriter(fos);
			
			//带有默认缓冲的字符输入流
			BufferedWriter writer=new BufferedWriter(dos);
			
			//整型  音乐目录
			for(int i=0;i<musicList.size();i++){
				Music music=(Music)musicList.get(i);
				writer.write(music.getMusicTheme()+" "+music.getMusicTime()+" "+music.getMusicMusician()+" "+music.getCriticism()+"\n");
			}
			writer.close();
			dos.close();
			fos.close();
			
			//在不存在具有指定路径名的文件时抛出此异常
		} catch (FileNotFoundException e) {
			
			//打印异常
			e.printStackTrace();
			
			//（在读写数据时）发生 I/O 错误时引发的异常
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void readCommentFile(){
		try{
			
			//FileInputStream 用于读取本地文件中的字节数据
			FileInputStream fis;
			fis = new FileInputStream("Comment.txt");
			
			//InputStreamReader()方法是字节流通向字符流的桥梁，它使用指定的 charset读取字节并将其解码为字符
			InputStreamReader dis=new InputStreamReader(fis);
			
			//BufferedReader 由Reader类扩展而来，提供通用的缓冲方式文本读取，而且提供了很实用的readLine，读取一个文本行，从字符输入流中读取文本，缓冲各个字符，从而提供字符、数组和行的高效读取。
			BufferedReader reader=new BufferedReader(dis);
			String s;
			while((s=reader.readLine())!=null)
			{
				Comment comment=new Comment();
				String[] temp=s.split(" ");
				comment.setCommentAchievement(temp[0]);//票房
				comment.setCommentGrade(Double.parseDouble(temp[1]));//评分
				comment.setCinecism((temp[2]));//影评
				commentList.add(comment);//后期反馈菜单
			}
			reader.close();
			dis.close();
			fis.close();
			
			//（在读写数据时）发生 I/O 错误时引发的异常
		} catch (IOException e) {
			//打印异常
			e.printStackTrace();
		}
	}
	
	void writeCommentFile(){
		try {
			//文件字节输出流
			FileOutputStream fos = new FileOutputStream("Comment.txt");
			//将字节流转换为字符流
			OutputStreamWriter dos=new OutputStreamWriter(fos);
			//带有默认缓冲的字符输入流
			BufferedWriter writer=new BufferedWriter(dos);
			
			//整型  影评目录
			for(int i=0;i<commentList.size();i++){
				Comment comment=(Comment)commentList.get(i);
				//读入数据：“票房”“评分”“影评”
				writer.write(comment.getCommentAchievement()+" "+comment.getCommentGrade()+" "+String.format("%.1f", comment.getCinecism())+"\n");
			}
			writer.close();
			dos.close();
			fos.close();
			
			//在不存在具有指定路径名的文件时抛出此异常
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			
			//（在读写数据时）发生 I/O 错误时引发的异常
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//显示电影的面板控件，用于为其他控件提供可识别的分组
	void showFilmPanel(){
		FilmPanel filmPanel=new FilmPanel();
		filmPanel.filmList=this.filmList;
		
		//电影面板 边界布局，居中
		this.add(filmPanel,BorderLayout.CENTER);
	
	
	//电影面板展示电影数据
		filmPanel.showFilm(0);
		
		//设置可视化
		this.setVisible(true);
	}
	
	//显示音乐面板
	void showMusicPanel(){
		MusicPanel musicPanel=new MusicPanel();
		
		//音乐面板显示音乐歌曲菜单
		musicPanel.musicList=this.musicList;
		
		//音乐面板 边界布局，居中
		this.add(musicPanel,BorderLayout.CENTER);
		
		musicPanel.showMusic(0);
		
		//设置可视觉化
		this.setVisible(true);
	}
	
	
	//展示评论面板
	void showCommentPanel(){
		CommentPanel commentPanel=new CommentPanel();
		
		//评论面板显示评论列表
		commentPanel.commentList=this.commentList;
		
		//评论面板，边界布局，居中
		this.add(commentPanel,BorderLayout.CENTER);
		commentPanel.showComment(0);
		this.setVisible(true);
	}
	
	
	//展示电影资源查询面板
	void showFilmQueryPanel(){
		FilmQueryPanel filmQPanel=new FilmQueryPanel();
		filmQPanel.filmList=this.filmList;
		filmQPanel.musicList=this.musicList;
		filmQPanel.commentList=this.commentList;
		this.add(filmQPanel, BorderLayout.CENTER);
		this.setVisible(true);
	}

	//展示电影影评查询面板
	void showCommentQueryPanel(){
		CommentQueryPanel commentQPanel=new CommentQueryPanel();
		commentQPanel.filmList=this.filmList;
		commentQPanel.musicList=this.musicList;
		commentQPanel.commentList=this.commentList;
		this.add(commentQPanel, BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	
	//actionPerformed 事件监听器，可以处理类似单击鼠标时触发的事件ActionEvent就是一个事件类，传入的e就是该事件的对象
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filmMenu){
			
			//通过顶层容器的getContentPane()方法获得内容窗格，从而将组件放入内容窗格中
			this.getContentPane().removeAll();
			this.showFilmPanel();
		}
		if(e.getSource()==musicMenu){
			this.getContentPane().removeAll();
			this.showMusicPanel();
		}
		if(e.getSource()==commentMenu){
			this.getContentPane().removeAll();
			this.showCommentPanel();
		}
		if(e.getSource()==filmQueryMenu){
			this.getContentPane().removeAll();
			this.showFilmQueryPanel();
		}
		//将组件放入内容窗格
		if(e.getSource()==commentQueryMenu){
			this.getContentPane().removeAll();
			this.showCommentQueryPanel();
		}
		if(e.getSource()==menuOpen){
			
			//内存重新分配，直至清零，从而将组件放入内容窗格中
			this.filmList.removeAllElements();
			this.musicList.removeAllElements();
			this.commentList.removeAllElements();
			this.readFilmFile();
			filmMenu.setEnabled(true);
			this.readMusicFile();
			musicMenu.setEnabled(true);
			this.readCommentFile();
			commentMenu.setEnabled(true);
			menuSave.setEnabled(true);
			filmQueryMenu.setEnabled(true);
			commentQueryMenu.setEnabled(true);
			//显示数据信息
			JOptionPane.showMessageDialog(null, "您已经成功打开数据：\n影片信息"+filmList.size()+"条\n音乐信息"+musicList.size()+"条\n评价信息"+commentList.size()+"条", "打开", JOptionPane.INFORMATION_MESSAGE);
		}
		
		//数据保存
		if(e.getSource()==menuSave){
			this.writeFilmFile();
			this.writeMusicFile();
			menuSave.setEnabled(true);
			musicMenu.setEnabled(true);
			JOptionPane.showMessageDialog(null, "您已经成功保存数据：\n影片信息"+filmList.size()+"条\n音乐信息"+musicList.size()+"条\n评价信息"+commentList.size()+"条", "保存", JOptionPane.INFORMATION_MESSAGE);
		}
		if(e.getSource()==menuChangePwd){
			String s=JOptionPane.showInputDialog(null, "请输入新密码", "管理员密码修改", JOptionPane.PLAIN_MESSAGE);
			if(s==null)return;
			s=s.trim();
			if(s.length()==0){
				JOptionPane.showMessageDialog(null, "密码不能为空！", "电影资源管理系统", JOptionPane.ERROR_MESSAGE);
				return;
			}
			String clearText = 	"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			String cipherText=	"UADKIy3FxgVkl5iZzWuGd1HNhOCtvjJ2pEn6Yw7PqrcQReB8Mfm0STsLX9a4ob";
			String resultText=		"";
			for(int i=0;i<s.length();i++){
				char c=s.charAt(i);
				
				//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现，则该方法返回 -1。
				if(clearText.indexOf(""+c)==-1){
					
					//JOptionPane类属于javax.swing 包， 显示信息对话框
					JOptionPane.showMessageDialog(null, "密码中包含非法字符", "电影资源管理系统", JOptionPane.ERROR_MESSAGE);
					return;
				}else{
					resultText+=""+cipherText.charAt(clearText.indexOf(""+c));
				}
			}
			try {
				FileOutputStream fos = new FileOutputStream("Password.txt");
				OutputStreamWriter dos=new OutputStreamWriter(fos);
				BufferedWriter writer=new BufferedWriter(dos);
				writer.write(resultText);
				writer.close();dos.close();fos.close();
				JOptionPane.showMessageDialog(null, "密码修改成功！", "电影资源管理系统", JOptionPane.INFORMATION_MESSAGE);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}			
		if(e.getSource()==menuExit){
			System.exit(0);
		}
	}
}

@SuppressWarnings("serial")
class CommentPanel extends JPanel implements ActionListener{
	private JTextField commentAchievement=new JTextField();							//票房
	private JTextField commentGrade=new JTextField();						//评分
	private JTextField cinecism=new JTextField();							//影评
	Vector<Comment> commentList=new Vector<Comment>();
	int count=0,current=0,inserting=0;
	
	private String[] btnStr={"第一个","上一个","下一个","最后一个","添加","修改","删除"};
	private JButton[] btn= new JButton[btnStr.length];
	
	CommentPanel(){
		this.setLayout(null);
		//显示学号
		JLabel lb1=new JLabel("票房：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		commentAchievement.setBounds(100,10, 100, 20);
		this.add(commentAchievement);
		//显示课程代号
		JLabel lb2=new JLabel("评分：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		commentGrade.setBounds(100,60, 100, 20);
		this.add(commentGrade);
		//显示成绩
		JLabel lb3=new JLabel("影评：");
		lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		cinecism.setBounds(100,110, 100, 20);
		this.add(cinecism);
		
		for(int i=0;i<btn.length;i++){
			btn[i]=new JButton(btnStr[i]);
			btn[i].setBounds(30+i*90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}
		
	}
	
	void showComment(int offset){
		Comment comment=(Comment)commentList.get(offset);
		this.commentAchievement.setText(comment.getCommentAchievement());
		this.commentGrade.setText(""+comment.getCommentGrade());
		this.cinecism.setText(""+comment.getCinecism());
	}

	public void actionPerformed(ActionEvent e) {
		count=this.commentList.size();
		if(e.getSource()==this.btn[0]){
			this.showComment(0);
			current=0;
		}
		if(e.getSource()==this.btn[1]&&current>0){
			this.showComment(current-1);
			current=current-1;
		}
		if(e.getSource()==this.btn[2]&&current<count-1){
			this.showComment(current+1);
			current=current+1;
		}
		if(e.getSource()==this.btn[3]){
			this.showComment(count-1);
			current=count-1;
		}
		if(e.getSource()==this.btn[4]){
			if(this.inserting==0){
				this.commentAchievement.setText("");
				this.commentGrade.setText("");
				this.cinecism.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting=1;
			}
			else{
				Comment comment=new Comment();
				comment.setCommentAchievement(this.commentAchievement.getText().trim());
				comment.setCommentGrade(Double.parseDouble(this.commentGrade.getText().trim()));
				comment.setCinecism((this.cinecism.getText().trim()));
				commentList.add(comment);
				count++;
				current=count-1;
				btn[4].setText("添加");
				this.inserting=0;
			}
			for(int i=0;i<btn.length;i++){
				if(i==4||i==5) continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}			
		}
		if(e.getSource()==this.btn[5]){
			if(this.inserting==0){
				Comment comment=(Comment)commentList.get(current);
				comment.setCommentAchievement(this.commentAchievement.getText().trim());
				comment.setCommentGrade(Double.parseDouble(this.commentGrade.getText().trim()));
				comment.setCinecism((this.cinecism.getText().trim()));
			}else{
				btn[4].setText("添加");
				btn[5].setText("修改");
				for(int i=0;i<btn.length;i++){
					if(i==4||i==5) continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}	
				this.inserting=0;
				this.showComment(current);
			}
		}
		if(e.getSource()==this.btn[6]){
			if(count==0)
				return;
			commentList.remove(current);
			count--;
			if(count==0){
				this.commentAchievement.setText("");
				this.commentGrade.setText("");
				this.cinecism.setText("");
			}else{
				if(current>count-1){
					this.showComment(current-1);
					current=current-1;
				}
				else
					this.showComment(current);
			}
		}
		this.repaint();
	}
}

@SuppressWarnings("serial")
class FilmPanel extends JPanel implements ActionListener {
	private JTextField filmName=new JTextField();											//影片名
	private JTextField filmDirector=new JTextField();										//导演
	private JTextField filmTime=new JTextField();											//上映时间
	private JTextField filmActors=new JTextField();								//演员
	Vector<Film> filmList=new Vector<Film>();
	int count=0,current=0,inserting=0;
	
	private String[] btnStr={"第一个","上一个","下一个","最后一个","添加","修改","删除"};
	private JButton[] btn= new JButton[btnStr.length];
	
	FilmPanel(){
		this.setLayout(null);
		//显示学号
		JLabel lb1=new JLabel("影片名：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		filmName.setBounds(100,10, 100, 20);
		this.add(filmName);
		//显示姓名
		JLabel lb2=new JLabel("导演：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		filmDirector.setBounds(100,60, 100, 20);
		this.add(filmDirector);
		//显示性别
		JLabel lb3=new JLabel("编号：");
		lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		filmTime.setBounds(100,110, 100, 20);
		this.add(filmTime);
		//显示出生日期
		JLabel lb4=new JLabel("演员：");
		lb4.setBounds(30, 160, 100, 20);
		this.add(lb4);
		filmActors.setBounds(100,160, 100, 20);
		this.add(filmActors);
		
		for(int i=0;i<btn.length;i++){
			btn[i]=new JButton(btnStr[i]);
			btn[i].setBounds(30+i*90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}
	}
	
	void showFilm(int offset){
		Film film=(Film) filmList.get(offset);
		this.filmName.setText(film.getFilmName());
		this.filmDirector.setText(film.getFilmDirector());
		this.filmTime.setText(film.getFilmTime());
		this.filmActors.setText(film.getFilmActors());
	}

	public void actionPerformed(ActionEvent e) {
		count=this.filmList.size();
		if(e.getSource()==this.btn[0]){
			this.showFilm(0);
			current=0;
		}
		if(e.getSource()==this.btn[1]&&current>0){
			this.showFilm(current-1);
			current=current-1;
		}
		if(e.getSource()==this.btn[2]&&current<count-1){
			this.showFilm(current+1);
			current=current+1;
		}
		if(e.getSource()==this.btn[3]){
			this.showFilm(count-1);
			current=count-1;
		}
		if(e.getSource()==this.btn[4]){
			if(this.inserting==0){
				this.filmName.setText("");
				this.filmDirector.setText("");
				this.filmTime.setText("");
				this.filmActors.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting=1;
			}else{
				Film film=new Film();
				film.setFilmName(this.filmName.getText().trim());
				film.setFilmDirector(this.filmDirector.getText().trim());
				film.setFilmTime(this.filmTime.getText().trim());
				film.setFilmActors(this.filmActors.getText().trim());
				filmList.add(film);
				count++;
				current=count-1;
				btn[4].setText("添加");
				this.inserting=0;
			}
			for(int i=0;i<btn.length;i++){
				if(i==4||i==5) continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}	
		}

		if(e.getSource()==this.btn[5]){
			if(this.inserting==0){
				Film film=(Film)filmList.get(current);
				film.setFilmName(this.filmName.getText().trim());
				film.setFilmDirector(this.filmDirector.getText().trim());
				film.setFilmTime(this.filmTime.getText().trim());
				film.setFilmActors(this.filmActors.getText().trim());
			}else{
				btn[4].setText("添加");
				btn[5].setText("修改");
				for(int i=0;i<btn.length;i++){
					if(i==4||i==5) continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}
				this.inserting=0;
				this.showFilm(current);
			}
		}
		if(e.getSource()==this.btn[6]){
			if(count==0)
				return;
			filmList.remove(current);
			count--;
			if(count==0){
				this.filmName.setText("");
				this.filmDirector.setText("");
				this.filmTime.setText("");
				this.filmActors.setText("");
			}else{
				if(current>count-1){
					this.showFilm(current-1);
					current=current-1;
				}
				else
					this.showFilm(current);
			}
		}
		this.repaint();
	}
}

@SuppressWarnings("serial")
class MusicPanel extends JPanel implements ActionListener {
	private JTextField musicTheme=new JTextField();										//
	private JTextField musicTime=new JTextField();										//课程名称
	private JTextField musicMusician=new JTextField();										//学分
	private JTextField criticism=new JTextField();												//任课教师
	Vector<Music> musicList=new Vector<Music>();
	int count=0,current=0,inserting=0;
	
	private String[] btnStr={"第一个","上一个","下一个","最后一个","添加","修改","删除"};
	private JButton[] btn= new JButton[btnStr.length];
	
	MusicPanel(){
		this.setLayout(null);
		//显示课程代号
		JLabel lb1=new JLabel("主题曲：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		musicTheme.setBounds(100,10, 100, 20);
		this.add(musicTheme);
		//显示课程名
		JLabel lb2=new JLabel("时长：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		musicTime.setBounds(100,60, 100, 20);
		this.add(musicTime);
		//显示学分
		JLabel lb3=new JLabel("歌手：");
1.	lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		musicMusician.setBounds(100,110, 100, 20);
		this.add(musicMusician);
		//显示任课教师
		JLabel lb4=new JLabel("乐评：");
		lb4.setBounds(30, 160, 100, 20);
		this.add(lb4);
		criticism.setBounds(100,160, 100, 20);
		this.add(criticism);
		
		for(int i=0;i<btn.length;i++){
			btn[i]=new JButton(btnStr[i]);
			btn[i].setBounds(30+i*90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}
	}
	
	void showMusic(int offset){
		Music music=(Music) musicList.get(offset);
		this.musicTheme.setText(music.getMusicTheme());
		this.musicTime.setText(music.getMusicTime());
		this.musicMusician.setText(""+music.getMusicMusician());
		this.criticism.setText(music.getCriticism());
	}

	public void actionPerformed(ActionEvent e) {
		count=this.musicList.size();
		if(e.getSource()==this.btn[0]){
			this.showMusic(0);
			current=0;
		}
		if(e.getSource()==this.btn[1]&&current>0){
			this.showMusic(current-1);
			current=current-1;
		}
		if(e.getSource()==this.btn[2]&&current<count-1){
			this.showMusic(current+1);
			current=current+1;
		}
		if(e.getSource()==this.btn[3]){
			this.showMusic(count-1);
			current=count-1;
		}
		if(e.getSource()==this.btn[4]){
			if(this.inserting==0){
				this.musicTheme.setText("");
				this.musicTime.setText("");
				this.musicMusician.setText("");
				this.criticism.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting=1;
			}else{
				Music music=new Music();
				music.setMusicTheme(this.musicTheme.getText().trim());
				music.setMusicTime(this.musicTime.getText().trim());
				music.setMusicMusician((this.musicMusician.getText().trim()));
				music.setCriticism(this.criticism.getText().trim());
				musicList.add(music);
				count++;
				current=count-1;
				btn[4].setText("添加");
				this.inserting=0;
			}
			for(int i=0;i<btn.length;i++){
				if(i==4||i==5) continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}	
		}

		if(e.getSource()==this.btn[5]){
			if(this.inserting==0){
				Music music=(Music)musicList.get(current);
				music.setMusicTheme(this.musicTheme.getText().trim());
				music.setMusicTime(this.musicTime.getText().trim());
				music.setMusicMusician(this.musicMusician.getText().trim());
				music.setCriticism(this.criticism.getText().trim());
			}else{
				btn[4].setText("添加");
				btn[5].setText("修改");
				for(int i=0;i<btn.length;i++){
					if(i==4||i==5) continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}
				this.inserting=0;
				this.showMusic(current);
			}
		}
		if(e.getSource()==this.btn[6]){
			if(count==0)
				return;
			musicList.remove(current);
			count--;
			if(count==0){
				this.musicTheme.setText("");
				this.musicTime.setText("");
				this.musicMusician.setText("");
				this.criticism.setText("");
			}else{
				if(current>count-1){
					this.showMusic(current-1);
					current=current-1;
				}
				else
					this.showMusic(current);
			}
		}
		this.repaint();
	}
}

@SuppressWarnings("serial")
class FilmQueryPanel extends JPanel implements ActionListener{
	JComboBox searchBy=new JComboBox();
	JTextField keyword=new JTextField(10);
	JTextArea result=new JTextArea();
	Vector<Film> filmList=new Vector<Film>();
	Vector<Comment> commentList=new Vector<Comment>();
	Vector<Music> musicList=new Vector<Music>();
	FilmQueryPanel(){
		this.setLayout(new BorderLayout());
		JPanel subPanel=new JPanel();
		subPanel.setLayout(new FlowLayout());
		searchBy.addItem("按影片名查询");
		searchBy.addItem("按导演查询");		
		subPanel.add(searchBy);
		subPanel.add(keyword);
		JButton btn=new JButton("查询");
		btn.addActionListener(this);
		subPanel.add(btn);
		this.add(subPanel, BorderLayout.NORTH);
		JScrollPane scrollPane=new JScrollPane(result);
		this.add(scrollPane, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent arg0) {
		String str="电影名\t导演\t编号\t演员\n";
		for(int i=0;i<filmList.size();i++){
			Film film=(Film)this.filmList.get(i);
			if(this.searchBy.getSelectedIndex()==0){
				if(film.getFilmName().startsWith(this.keyword.getText().trim())||this.keyword.getText().trim().equals("")){
					str=str+film.getFilmName()+"\t"+film.getFilmDirector()+"\t"+film.getFilmTime()+"\t"+film.getFilmActors()+"\t";
				}else
					continue;
			}if(this.searchBy.getSelectedIndex()==1){
				if(film.getFilmDirector().startsWith(this.keyword.getText().trim())||this.keyword.getText().trim().equals("")){
					str=str+film.getFilmName()+"\t"+film.getFilmDirector()+"\t"+film.getFilmTime()+"\t"+film.getFilmActors()+"\t";
				}else
					continue;
			}
			for(int j=0;j<commentList.size();j++){
				Comment comment=(Comment)commentList.get(j);
				if(comment.getCinecism().equals(film.getFilmName())){
					str=str+this.getMusicTheme(comment.getCommentAchievement())+"："+comment.getCommentGrade()+"  ";
				}
			}
			str+="\n";
		}
		this.result.setText(str);
	}
	
	String getMusicTheme(String musicNo){
		for(int i=0;i<musicList.size();i++){
			Music music=(Music)this.musicList.get(i);
			if(musicNo.equals(music.getMusicTheme())){
				return music.getMusicTheme();
			}
		}
		return null;
	}
}
@SuppressWarnings("serial")
class CommentQueryPanel extends JPanel implements ActionListener{
	JTextField keyword=new JTextField(10);
	JTextArea result=new JTextArea();
	Vector<Film> filmList=new Vector<Film>();
	Vector<Music> musicList=new Vector<Music>();
	Vector<Comment> commentList=new Vector<Comment>();
	CommentQueryPanel(){
		this.setLayout(new BorderLayout());
		JPanel subPanel=new JPanel();
		subPanel.setLayout(new FlowLayout());
		subPanel.add(new JLabel("电影编号"));
		subPanel.add(keyword);
		JButton btn=new JButton("查询");
		btn.addActionListener(this);
		subPanel.add(btn);
		this.add(subPanel, BorderLayout.NORTH);
		JScrollPane scrollPane=new JScrollPane(result);
		this.add(scrollPane, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent arg0) {
		if(keyword.getText().length()!=2){
			JOptionPane.showMessageDialog(null, "请输入正确的电影编号", "电影资源管理系统", JOptionPane.ERROR_MESSAGE);
			return;
		}
		double max;
		int count;
		String str="电影名称\t"+ "导演\t"+"评分\n";
		Film film=null;
		Comment comment=null;
		for(int i=0;i<filmList.size();i++){
			film=filmList.get(i);
			max=0;count=0;
			for(int j=0;j<commentList.size();j++){
				comment=commentList.get(j);
				if(film.getFilmTime().startsWith(this.keyword.getText().trim())&&film.getFilmTime().equals(comment.getCinecism())){
					if(comment.getCommentGrade()>max)
						max=comment.getCommentGrade();
			
					count++;
				}
			}
			if(count!=0){
				DecimalFormat df=new DecimalFormat("#.0");
				str+=film.getFilmName()+"\t"+film.getFilmDirector()+"\t"+df.format(max)+"\t"+"\n";
			}
		}
		
		this.result.setText(str);
	}
}

class Film {
	private String filmName;									//影片名
	private String filmDirector;								//导演
	private String filmTime;									//编号
	private String filmActors;									//演员
	Comment comment;											
	
	public String getFilmName() {								//获取影片名
		return filmName;
	}
	public void setFilmName(String filmName) {					//设置影片名
		this.filmName = filmName;
	}
	public String getFilmDirector() {							//获取导演信息
		return filmDirector;
	}
	public void setFilmDirector(String filmDirector) {			//设置导演信息
		this.filmDirector = filmDirector;
	}
	public String getFilmTime() {								//获取编号
		return filmTime;
	}
	public void setFilmTime(String filmTime) {					//设置编号
		this.filmTime = filmTime;
	}
	public String getFilmActors() {								//获取演员信息
		return filmActors;
	}
	public void setFilmActors(String filmActors) {				//设置演员信息
		this.filmActors = filmActors;
	}
}

class Music{
	private String musicTheme;									//主题曲
	private String musicTime;									//时长
	private String musicMusician;								//歌手
	private String criticism;									//乐评
	
	public String getMusicTheme() {								//获取主题曲
		return musicTheme;
	}
	public void setMusicTheme(String musicTheme) {				//设置主题曲
		this.musicTheme = musicTheme;
	}
	public String getMusicTime() {								//获取时长
		return musicTime;
	}
	public void setMusicTime(String musicTime) {				//设置时长
		this.musicTime = musicTime;
	}
	public String getMusicMusician() {							//获取歌手信息
		return musicMusician;
	}
	public void setMusicMusician(String musicMusician) {		//设置歌手信息
		this.musicMusician = musicMusician;
	}
	public String getCriticism() {								//获取乐评
		return criticism;
	}
	public void setCriticism(String criticism) {				//设置乐评
		this.criticism = criticism;
	}
}

class Comment{
	private String commentAchievement;							//票房
	private double commentGrade;								//评分
	private String cinecism;									//编号
	
	public String getCommentAchievement() {						//获取票房
		return commentAchievement;
	}
	public double getComment() {
		// TODO 自动生成的方法存根
		return 0;
	}
	
	public void setCommentAchievement(String commentAchievement) {//设置票房
		this.commentAchievement = commentAchievement;
	}
	public double getCommentGrade() {							//获取评分
		return commentGrade;
	}
	public void setCommentGrade(double commentGrade) {			//设置评分
		this.commentGrade = commentGrade;
	}
	public String getCinecism() {								//获取编号
		return cinecism;
	}
	public void setCinecism(String cinecism) {					//设置编号
		this.cinecism = cinecism;
	}
}

@SuppressWarnings("serial")
class LoginFrame extends JFrame implements ActionListener{		//定义LoginFrame类
	JTextField username=new JTextField(10);						//创建用户名框
	JPasswordField pwd=new JPasswordField(10);					//创建密码框
	LoginFrame(){
		this.setTitle("系统登录");//
		this.setResizable(false);
		this.setSize(250,130);
		this.setLocationRelativeTo(this.getOwner());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel lb1=new JLabel("用户名：");
		JLabel lb2=new JLabel("密　码：");
		JButton btn=new JButton("登录");
		btn.addActionListener(this);
		Container c=this.getContentPane();
		c.setLayout(null);
		lb1.setBounds(30, 10, 100, 20);
		c.add(lb1);
		username.setBounds(120, 10, 100, 20);
		username.addActionListener(this);
		c.add(username);
		lb2.setBounds(30, 40, 100, 20);
		c.add(lb2);
		pwd.setBounds(120, 40, 100, 20);
		pwd.addActionListener(this);
		c.add(pwd);
		btn.setBounds(80, 70, 90, 20);
		c.add(btn);
		this.setVisible(true);
		this.username.requestFocus();
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.username){
			this.pwd.requestFocus();
			return;
		}
		try {
			FileInputStream fis;
			fis = new FileInputStream("Password.txt");
			InputStreamReader dis=new InputStreamReader(fis);
			BufferedReader reader=new BufferedReader(dis);
			String s;
			String clearText = 	"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			String cipherText=	"UADKIy3FxgVkl5iZzWuGd1HNhOCtvjJ2pEn6Yw7PqrcQReB8Mfm0STsLX9a4ob";
			if((s=reader.readLine())!=null){
				if(username.getText().trim().equals("admin")){
					boolean isCorrect=true;
					char[] ch1=pwd.getPassword();
					char[] ch2=s.toCharArray();
					if(ch1.length==ch2.length){
						for(int i=0;i<ch1.length;i++){
							if(clearText.indexOf(ch1[i])!=cipherText.indexOf(ch2[i])){
								isCorrect=false;
								break;
							}
						}
					}else{
						isCorrect=false;
					}
					if(isCorrect){
						this.setVisible(false);
						this.dispose();
						MainFrame4 f=new MainFrame4();
						f.setVisible(true);
					}else{
						JOptionPane.showMessageDialog(null, "您填写的密码不正确", "用户登录", JOptionPane.WARNING_MESSAGE);
					}
				}else{
					JOptionPane.showMessageDialog(null, "您填写的用户名不正确", "用户登录", JOptionPane.WARNING_MESSAGE);
					this.username.requestFocus();
				}
			}
			reader.close();
			dis.close();
			fis.close();
		}catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}
